<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改信息收集')" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-detailegather-edit" th:object="${cdDetailegather}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">类型：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('cd_msgtype')}">
                        <input type="radio" th:id="${'typeId_' + dict.dictCode}" name="typeId" th:value="${dict.dictValue}" th:field="*{typeId}" required>
                        <label th:for="${'typeId_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('cd_status')}">
                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="status" th:value="${dict.dictValue}" th:field="*{status}" required>
                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>

<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label is-required">信息记录2：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <textarea name="tomorrowWork" class="form-control" rows="3" required>[[*{tomorrowWork}]]</textarea>-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">信息记录：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{bakContent}">
                    <div class="summernote" id="bakContent" required></div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea name="todayWork" class="form-control" rows="5" >[[*{todayWork}]]</textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">附件列表：</label>
                <div class="col-sm-8">
                    <div id="ext3" style="display: none;">[[*{ext3}]]</div>
                    <div id="ext4" style="display: none;">[[*{ext4}]]</div>
                    <ul id="fileList"></ul>
                </div>
            </div>
            <div class="form-group">
                <label class="font-noraml">附件上传</label>
                <div class="file-loading">
                    <input id="multipleFile" name="files" type="file" multiple>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script th:inline="javascript">
        var prefix = ctx + "cd/detailegather";
        var rspUrls = ""//附件url
        var rspNewFileNames = ""//附件名称
        $("#form-detailegather-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            //1、校验是否未上传附件
            if($('#multipleFile')[0].files[0]!=undefined){
                $.modal.msgError("含有未上传的文件，请先上传文件后再提交")
                return
            }
            //2、序列号表单元素，且剔除原有附件参数
            var msg = $("#form-detailegather-edit").serialize()
            //3、拼接附件参数
            var ext3Value = $('#ext3').html()
            var ext4Value = $('#ext4').html()
            if($('#ext3').html()!=''&&rspNewFileNames!=''){
                ext3Value = $('#ext3').html() + ',' +rspNewFileNames
                ext4Value = $('#ext4').html() + ',' +rspUrls
            }else if($('#ext3').html()==''&&rspNewFileNames!=''){
                ext3Value = rspNewFileNames
                ext4Value = rspUrls
            }
            //4、将新生成的参数进行拼接
            msg = msg + '&ext3='+ext3Value+'&ext4='+ext4Value;
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", msg);
            }
        }

        $(function() {
            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    dialogsInBody: true,
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });

        $(document).ready(function () {
            // 多文件上传
            $("#multipleFile").fileinput({
                uploadUrl: ctx + 'common/uploads',
                uploadAsync: false
            }).on('filebatchuploadsuccess', function (event, data, previewId, index) {
                var rsp = data.response;
                log.info("return urls：" + rsp.urls)//http://localhost:8089/profile/upload/2024/04/29/SSS.png,http://localhost:8089/profile/upload/2024/04/29/viewImage_20240429175810A004.png
                log.info("reutrn fileNames：" + rsp.fileNames)
                log.info("reutrn newFileNames：" + rsp.newFileNames)//SSS.png,viewImage_20240429175810A004.png
                log.info("return originalFilenames：" + rsp.originalFilenames)
                rspUrls = rsp.urls
                rspNewFileNames = rsp.originalFilenames
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            chaifen()
        });
        //拼接附件信息到页面中
        function chaifen( ){
            const fileNameList = $('#ext3').html().split(',')
            const fileList = $('#ext4').html().split(',')
            const fileListContainer = document.getElementById('fileList');
            if(fileList!=''){
                fileList.forEach((file,index) => {
                    const listItem = document.createElement('li');
                    listItem.id = "li_"+index
                    listItem.innerHTML = "<a href="+file+">"+fileNameList[index]+"</a><i class=\"fa fa-remove\" onclick=\"removeFile("+index+",'"+fileNameList[index]+"','"+file+"')\"></i>";
                    fileListContainer.appendChild(listItem);
                });
            }
        }
        //根据下标删除数组附件中元素，并重新刷新信息
        function removeFile(delete_index,fileName,fileHref ){
            //1、移除显示的ul中附件信息
            const fileListContainer = document.getElementById('fileList');
            const deleteM = document.getElementById('li_'+delete_index);
            fileListContainer.removeChild(deleteM);

            // 2、再按照名称删除附件字段中信息
            let fileNameList = $('#ext3').html().split(',');
            var fileList = $('#ext4').html().split(',')
            fileNameList = fileNameList.filter(item => item !== fileName);
            fileList = fileList.filter(item => item !== fileHref);

            //3、回写附件字段中信息
            document.getElementById('ext3').innerHTML = fileNameList
            document.getElementById('ext4').innerHTML = fileList
        }

    </script>
</body>
</html>
